<!DOCTYPE html>
<html>

<head><meta name="generator" content="Hexo 3.8.0">
  
  <title>Vue 多语言切换 - 绪锋</title>
  <meta charset="UTF-8">
  <meta name="description" content="小镇青年,心怀远方">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  

    <!-- Site Verification -->
    <meta name="baidu-site-verification" content="cXpXJOvmtY">

  <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon">
  <meta name="description" content="Vue I18n 是 Vue.js 的国际化插件。它可以轻松地将一些本地化功能集成到你的 Vue.js 应用程序中。">
<meta name="keywords" content="vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue 多语言切换">
<meta property="og:url" content="https://mraing.github.io/2019/12/11/Vue-多语言切换/index.html">
<meta property="og:site_name" content="绪锋">
<meta property="og:description" content="Vue I18n 是 Vue.js 的国际化插件。它可以轻松地将一些本地化功能集成到你的 Vue.js 应用程序中。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://images.leegeing.cn/hexoImg/l18n-lang.gif">
<meta property="og:updated_time" content="2021-03-14T13:54:47.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue 多语言切换">
<meta name="twitter:description" content="Vue I18n 是 Vue.js 的国际化插件。它可以轻松地将一些本地化功能集成到你的 Vue.js 应用程序中。">
<meta name="twitter:image" content="http://images.leegeing.cn/hexoImg/l18n-lang.gif">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/mdui@0.4.3/dist/css/mdui.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.15.8/styles/atom-one-dark.css">
   
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
  
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1038733_0xvrvpg9c0r.css">
  <link rel="stylesheet" href="/css/style.css?v=1627371788906">
</head>

<body class="mdui-drawer-body-left">
  
  <div id="nexmoe-background">
    <div class="nexmoe-bg" style="background-image: url(https://i.loli.net/2019/01/13/5c3aec85a4343.jpg)"></div>
    <div class="mdui-appbar mdui-shadow-0">
      <div class="mdui-toolbar">
        <a mdui-drawer="{target: '#drawer', swipe: true}" title="menu" class="mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">menu</i></a>
        <div class="mdui-toolbar-spacer"></div>
        <!--<a href="javascript:;" class="mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">search</i></a>-->
        <a href="/" title="LeeGeing" class="mdui-btn mdui-btn-icon"><img src="/images/avatar.jpg"></a>
       </div>
    </div>
  </div>
  <div id="nexmoe-header">
      <div class="nexmoe-drawer mdui-drawer" id="drawer">
    <div class="nexmoe-avatar mdui-ripple">
        <a href="/" title="LeeGeing">
            <img src="/images/avatar.jpg" alt="LeeGeing">
        </a>
    </div>
    <div class="nexmoe-count">
        <div><span>文章</span>82</div>
        <div><span>标签</span>28</div>
        <div><span>分类</span>8</div>
    </div>
    <ul class="nexmoe-list mdui-list" mdui-collapse="{accordion: true}">
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/" title="回到首页">
            <i class="mdui-list-item-icon nexmoefont icon-home"></i>
            <div class="mdui-list-item-content">
                回到首页
            </div>
        </a>
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/about.html" title="关于博客">
            <i class="mdui-list-item-icon nexmoefont icon-info-circle"></i>
            <div class="mdui-list-item-content">
                关于博客
            </div>
        </a>
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple" href="/PY.html" title="我的朋友">
            <i class="mdui-list-item-icon nexmoefont icon-unorderedlist"></i>
            <div class="mdui-list-item-content">
                我的朋友
            </div>
        </a>
        
    </ul>
    <aside id="nexmoe-sidebar">
  
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title">社交按钮</h3>
    <div class="nexmoe-widget nexmoe-social">
        <a class="mdui-ripple" href="https://space.bilibili.com/11147795" target="_blank" mdui-tooltip="{content: '哔哩哔哩'}" style="color: rgb(231, 106, 141);background-color: rgba(231, 106, 141, .15);">
            <i class="nexmoefont icon-bilibili"></i>
        </a><a class="mdui-ripple" href="https://github.com/mraing/" target="_blank" mdui-tooltip="{content: 'GitHub'}" style="color: rgb(25, 23, 23);background-color: rgba(25, 23, 23, .15);">
            <i class="nexmoefont icon-github"></i>
        </a>
    </div>
</div>
  
  
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title">文章分类</h3>
    <div class="nexmoe-widget">

      <ul class="category-list">

        


        

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/React/">React</a>
          <span class="category-list-count">7</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/TypeScript/">TypeScript</a>
          <span class="category-list-count">5</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/Vue/">Vue</a>
          <span class="category-list-count">33</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/前端/">前端</a>
          <span class="category-list-count">19</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/博客/">博客</a>
          <span class="category-list-count">3</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/工具/">工具</a>
          <span class="category-list-count">6</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/碎碎念/">碎碎念</a>
          <span class="category-list-count">4</span>
        </li>

        

        <li class="category-list-item">
          <a class="category-list-link" href="/categories/软件设计师/">软件设计师</a>
          <span class="category-list-count">5</span>
        </li>

        
      </ul>

    </div>
  </div>


  
  
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title">标签云</h3>
    <div id="randomtagcloud" class="nexmoe-widget tagcloud">
      <a href="/tags/Hbuilder/" style="font-size: 10px;">Hbuilder</a> <a href="/tags/Homebrew/" style="font-size: 10px;">Homebrew</a> <a href="/tags/JS/" style="font-size: 14px;">JS</a> <a href="/tags/JSX/" style="font-size: 10px;">JSX</a> <a href="/tags/Mint-UI/" style="font-size: 10px;">Mint-UI</a> <a href="/tags/Nginx/" style="font-size: 10px;">Nginx</a> <a href="/tags/Ngnix/" style="font-size: 10px;">Ngnix</a> <a href="/tags/OpenInTerminal/" style="font-size: 10px;">OpenInTerminal</a> <a href="/tags/Sass/" style="font-size: 14px;">Sass</a> <a href="/tags/Vuex/" style="font-size: 10px;">Vuex</a> <a href="/tags/antd/" style="font-size: 10px;">antd</a> <a href="/tags/css/" style="font-size: 10px;">css</a> <a href="/tags/docsify/" style="font-size: 10px;">docsify</a> <a href="/tags/git/" style="font-size: 12px;">git</a> <a href="/tags/hexo/" style="font-size: 12px;">hexo</a> <a href="/tags/mui/" style="font-size: 18px;">mui</a> <a href="/tags/react/" style="font-size: 14px;">react</a> <a href="/tags/redux/" style="font-size: 10px;">redux</a> <a href="/tags/swiper/" style="font-size: 10px;">swiper</a> <a href="/tags/typescript/" style="font-size: 16px;">typescript</a> <a href="/tags/vue/" style="font-size: 20px;">vue</a> <a href="/tags/vux/" style="font-size: 10px;">vux</a> <a href="/tags/她/" style="font-size: 10px;">她</a> <a href="/tags/感悟/" style="font-size: 12px;">感悟</a> <a href="/tags/日记/" style="font-size: 10px;">日记</a> <a href="/tags/消息推送/" style="font-size: 10px;">消息推送</a> <a href="/tags/知识点/" style="font-size: 10px;">知识点</a> <a href="/tags/软考笔记/" style="font-size: 16px;">软考笔记</a>
    </div>
    
  </div>

  
  
  <div class="nexmoe-widget-wrap">
    <h3 class="nexmoe-widget-title">文章归档</h3>
    <div class="nexmoe-widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">七月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/10/">十月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/09/">九月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/07/">七月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">六月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/04/">四月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/01/">一月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/12/">十二月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">九月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a></li></ul>
    </div>
  </div>


  
</aside>
    <!-- <div class="nexmoe-copyright">
        &copy; 2021 LeeGeing
        Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
        & <a href="https://nexmoe.com/hexo-theme-nexmoe.html" target="_blank">Nexmoe</a>
    </div> -->
    <div class="nexmoe-copyright">
        <a href="http://beian.miit.gov.cn/state/outPortal/loginPortal.action" target="_blank">湘ICP备16019481号</a>
        <!-- & <a href="https://nexmoe.com/hexo-theme-nexmoe.html" target="_blank">Nexmoe</a> -->
    </div>
</div><!-- .nexmoe-drawer -->
  </div>
  <div id="nexmoe-content">
    <div class="nexmoe-primary">
        <div class="nexmoe-post">
    <div class="nexmoe-post-cover"> 
        
            <img src="https://i.loli.net/2019/01/13/5c3aec85a4343.jpg">
        
        <h1>Vue 多语言切换</h1>
    </div>
  <div class="nexmoe-post-meta">
    <a><i class="nexmoefont icon-calendar-fill"></i>2019年12月11日</a>
    <a><i class="nexmoefont icon-areachart"></i>723 字</a>
    <a><i class="nexmoefont icon-time-circle-fill"></i>大概 3 分钟</a>
    
      <a class="nexmoefont icon-appstore-fill -link" href="/categories/Vue/">Vue</a>
    
    
      <a class="nexmoefont icon-tag-fill -link" href="/tags/vue/">vue</a>
    
  </div>
  <article>
    <blockquote>
<p>Vue I18n 是 Vue.js 的国际化插件。它可以轻松地将一些本地化功能集成到你的 Vue.js 应用程序中。</p>
</blockquote>
<a id="more"></a>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>我使用的是 npm 安装</p>
<pre><code class="shell">npm install vue-i18n
</code></pre>
<p>然后在 <code>main.js</code> 中引入 vue-i18n</p>
<pre><code class="javaScript">// main.js
import Vue from &#39;vue&#39;
import VueI18n from &#39;vue-i18n&#39;

Vue.use(VueI18n)
</code></pre>
<h2 id="新建语言文件"><a href="#新建语言文件" class="headerlink" title="新建语言文件"></a>新建语言文件</h2><p>新建一个文件夹 <code>l18n</code>（文件夹名字随意），用于存放语言文件。在文件夹中新建 <code>zh.js</code> 和 <code>ja.js</code> 文件。分别用于存放中文和日文的语言文件。</p>
<pre><code class="javaScript">// zh.js
module.exports = {
  message: {
    title: &#39;你好! 世界&#39;,
    hello: &#39;{msg}的世界&#39;,
    helloT: &#39;你好!{0}&#39;,
    helloH: &#39;你好! &lt;br&gt; 世界&#39;
  },
  placeholder: {
    enter: &#39;你想对世界说的话&#39;
  },
  meal: {
    breakfast: &#39;早餐&#39;,
    lunch: &#39;中餐&#39;,
    dinner: &#39;晚餐&#39;,
    supper: &#39;夜宵&#39;
  }
}

</code></pre>
<pre><code class="javaScript">// ja.js
// 注意：一定是 exports，不是 export，否则会报错，报错信息是下列的中的内容不是 string
module.exports = {
  message: {
    title: &#39;ハロー! 世界&#39;,
    hello: &#39;{msg}の世界&#39;,
    helloT: &#39;ハロー!{0}&#39;,
    helloH: &#39;ハロー! &lt;br&gt; 世界&#39;
  },
  placeholder: {
    enter: &#39;世界に言いたいことを入力してください&#39;
  },
  meal: {
    breakfast: &#39;朝食&#39;,
    lunch: &#39;ランチ&#39;,
    dinner: &#39;ディナー&#39;,
    supper: &#39;ナイトナイト&#39;
  }
}

</code></pre>
<h2 id="引用语言文件"><a href="#引用语言文件" class="headerlink" title="引用语言文件"></a>引用语言文件</h2><p>设置完语言文件后，在 <code>mian.js</code> 中进行引用</p>
<pre><code class="javaScript">const i18n = new VueI18n({
  locale: &#39;zh&#39;, // 默认语言标识
  messages: {
    &#39;ja&#39;: require(&#39;@/l18n/lang/ja&#39;), // 语言文件目录地址
    &#39;zh&#39;: require(&#39;@/l18n/lang/zh&#39;)  // 语言文件目录地址
  }
})

// 在 new 中新增i18n
new Vue({
  el: &#39;#app&#39;,
  ...
  i18n
})
</code></pre>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>在语言文件中的变量都是<code>$t(&#39;键名&#39;)</code>，接下来就可以在组件中使用了。</p>
<p><strong>直接使用</strong></p>
<pre><code class="HTML">&lt;!--.vue--&gt;
&lt;p&gt;{{ $t("message.title") }}&lt;/p&gt;

&lt;!--经过编译后就会变成--&gt;
&lt;p&gt;你好！世界&lt;/p&gt;
</code></pre>
<p>比如需要绑定某些属性</p>
<pre><code class="HTML">&lt;input type=&quot;text&quot; :placeholder=&quot;$t(&#39;message.title&#39;)&quot;&gt;

&lt;!--经过编译后就会变成--&gt;
&lt;input type=&quot;text&quot; placeholder=&quot;你好!世界&quot;&gt;
</code></pre>
<p><strong>变量使用</strong></p>
<p>我们可以看到 <code>message.hello</code> 的值里面有个花括号 <code>{msg}</code>,这里便是传入变量的地方</p>
<pre><code class="HTML">&lt;p&gt;{{ $t("message.hello", { msg: '绪锋' }) }}&lt;/p&gt;

&lt;!--经过编译后就会变成--&gt;
&lt;p&gt;绪锋的世界&lt;/p&gt;
</code></pre>
<p>还有一些其他的属性和方法，可以自己去官网查看，这就算一个简单的入门吧。</p>
<h2 id="更换语言选项"><a href="#更换语言选项" class="headerlink" title="更换语言选项"></a>更换语言选项</h2><blockquote>
<p>每个组件都包含一个引用为 <code>$i18n</code> 属性的 VueI18n 实例，该实例也可用于更改语言环境.</p>
</blockquote>
<p>每个组件都包含有一个 <code>$i18n.locale</code> 的属性，我们只需要更改这个属性的值，就达到了更换语言选项的目的。</p>
<pre><code class="HTML">// template
&lt;button @click=&quot;selectLang()&quot;&gt;中文/日文&lt;/button&gt;
</code></pre>
<pre><code class="JavaScript">// script
selectLang () {
  this.$i18n.locale = this.$i18n.locale === &#39;zh&#39; ? &#39;ja&#39; : &#39;zh&#39;
}
</code></pre>
<p>至此，一个简单的国际化就算完成了。</p>
<p><img src="http://images.leegeing.cn/hexoImg/l18n-lang.gif" alt="l18n-lang"></p>
<p><a href="https://github.com/mraing/vue-set/tree/master/vue-l18n" target="_blank" rel="noopener">项目源代码见 Github</a></p>

  </article>
  
    

  
  <section class="nexmoe-comment">
    <script id="dsq-count-scr" src="https://leegeing-cn.disqus.com/count.js" async></script>
<div id="disqus_thread"></div>
<script>
    var disqus_config = function () {
        this.page.url = 'https://mraing.github.io/2019/12/11/Vue-多语言切换/'; // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://mraing.github.io/2019/12/11/Vue-多语言切换/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>
<script id="disqus-thread-script">
    (function() { // DON'T EDIT BELOW THIS LINE
        var d = document;
        var s = d.createElement('script');
        s.src = '//leegeing-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', + new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>

</section>
</div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/mdui@0.4.3/dist/js/mdui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
 
    <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>


 
    <script src="https://cdn.jsdelivr.net/npm/smoothscroll-for-websites@1.4.9/SmoothScroll.min.js"></script>


<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.15.8/build/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>

<script src="/js/app.js?v=1627371788912"></script>
<script src="https://cdn.jsdelivr.net/npm/lazysizes@5.1.0/lazysizes.min.js"></script>


    <script type="text/javascript" src="https://cdn.jsdelivr.net/gh/xtaodada/xtaodada.github.io@0.0.2/copy.js"></script>

  





</body>

</html>
